'use strict';

// todoPage controller
app.controller('goodsInformation', ['$state','$filter','$window','$http','$scope','$modal','toaster','$log', function($state,$filter,$window,$http,$scope,$modal,toaster,$log) {
    $scope.title = {
        title:'货品资料列表',
        addTitle:'货品资料信息'
    };
    $scope.toaster = {
        type: 'warning',
        title: '提示：',
        text: '内容不能为空！'
    };
    $scope.pop = function(){
        toaster.pop($scope.toaster.type, $scope.toaster.title, $scope.toaster.text);
    };
    function showMessage(type,title,text){
        $scope.toaster.type = type;
        $scope.toaster.text = text;
        toaster.pop($scope.toaster.type, $scope.toaster.title, $scope.toaster.text);
    }
    function checkItems(){
        var items = ['hpmc','hpbh','hpxh','hpdj','zmkc','sjkc','hpdw','hplb'];
        var itemsValue = ['货品名称','货品编码','货品型号','货品单价','账面库存','实际库存','货品单位','货品类别'];
        for(var i=0;i<items.length;i++){
            if($('#'+items[i]+'').val()===''){
                $('#'+items[i]+'').focus();
                showMessage('warning','',itemsValue[i]+"不能为空！");
                return false;
            }
        }
        return true;
    }
    $scope.lists = ["","现金结算","银行转账"];
    $scope.mylabel = $scope.lists[0];//默认数组第一个作为option的值
    function addDo(){
        var notesdata = {};
        if(!$scope.row.businessId){
            notesdata = {
                "businessId":"",
                "qy":$("#qy").prop("checked"),
                "zdr":$scope.app.userName,
                "zdrId":$scope.app.userId,
                "businessNumber":$("#businessNumber").val(),
                "businessDwmc":$("#businessDwmc").val(),
                "businessDwjc":$("#businessDwjc").val(),
                "businessDwxz":$scope.lists[$("#businessDwxz").val()],
                "businessYhzh":$("#businessYhzh").val(),
                "businessSh":$("#businessSh").val(),
                "businessLxr":$("#businessLxr").val(),
                "businessDh":$("#businessDh").val(),
                "businessZw":$("#businessZw").val(),
                "businessBz":$("#businessBz").val(),
                "businessKhyh":$("#businessKhyh").val()
            }
        }else{
            notesdata = {
                "businessId":$scope.row.businessId,
                "qy":$("#qy").prop("checked"),
                "businessNumber":$("#businessNumber").val(),
                "businessDwmc":$("#businessDwmc").val(),
                "businessDwjc":$("#businessDwjc").val(),
                "businessDwxz":$scope.lists[$("#businessDwxz").val()],
                "businessYhzh":$("#businessYhzh").val(),
                "businessSh":$("#businessSh").val(),
                "businessLxr":$("#businessLxr").val(),
                "businessDh":$("#businessDh").val(),
                "businessZw":$("#businessZw").val(),
                "businessBz":$("#businessBz").val(),
                "businessKhyh":$("#businessKhyh").val()
            }
        }
        var info="";
        $.ajax({
            type: "post",
            url: '/a_yongan/add/purchase',
            dataType: "json",
            contentType:"application/json",
            data: JSON.stringify(notesdata),
            success: function(data) {
                if(data.success){
                    console.log(data);
                }else{
                    info = data.error;
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                info = "请求错误！";
                alert(XMLHttpRequest);
                alert(textStatus);
                alert(errorThrown);
            }
        });
        return info;
    }
    function getList(){
        $http({
            method: 'GET',
            url: '/a_yongan/list/purchase'
        }).success(function(data,status,headers,config) {
            if(data.success){
                makeData(data.data);
            }
        }).error(function(data,status,headers,config) {
            console.log('datae:',data)
        });
    }
    getList();
    function makeData(data){
        data.forEach(function (item) {
            item.createTime = $filter("date")(item.createTime, "yyyy/MM/dd");
            if(item.qy=='true'){
                item.qy = true;
                item.status = '使用中...';
                item.status1 = 'label bg-info';
            }else{
                item.qy = false;
                item.status = '禁用中...';
                item.status1 = 'label bg-danger';
            }
        })
        $scope.saveDate = data;
    }
    $scope.message = '是否确定保存？';
    $scope.open = function (size) {
        if(!checkItems()){
            return;
        }
        var modalInstance = $modal.open({
            templateUrl: 'myModalContent.html',
            controller: 'ModalInstanceCtrl',
            size: size,
            resolve: {
                items: function () {
                    return $scope.message;
                }
            }
        });
        modalInstance.result.then(function (selectedItem) {
            showMessage('success','','保存成功！');
            $scope.warehouseAdd = false;
            $scope.warehouseList = true;
            //$scope.selected = selectedItem;
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    };
    $scope.isshow=true;
    $scope.warehouseAdd = false;
    $scope.warehouseList = true;
    $scope.warehouseJump = function(path) {
        $scope.warhouseRow = {};
        $scope.warehouseAdd = !($scope.warehouseAdd);
        $scope.warehouseList = !($scope.warehouseList);
    };
    $scope.warhouseRow = {};
    $scope.columns = [
        {
            title:'创建时间',
            show:true
        },
        {
            title:'货品名称',
            show:true
        },
        {
            title:'货品编号',
            show:true
        },
        {
            title:'货品型号',
            show:true
        },
        {
            title:'货品单价(元)',
            show:true
        },
        {
            title:'货品类别',
            show:true
        },
        {
            title:'账面库存',
            show:true
        },
        {
            title:'实际库存',
            show:true
        },
        {
            title:'文件状态',
            show:true
        }
    ];
    $scope.showSelectDept = function(id){
        var modalInstance = $modal.open({
            templateUrl: 'selectDept.html',
            controller: 'selectDeptCtrl',
            size: 'sm',
            resolve: {
                items: function () {
                    return $scope.message;
                }
            }
        });
        modalInstance.result.then(function (selectedItem) {
            $('#'+id+'').val(selectedItem.name);
            showMessage('success','','选择成功！');
            //$scope.selected = selectedItem;
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    };
    $scope.saveDate=[
        {
            id:'1',
            createTime:'2018-11-12 10:30:39',
            hpmc:'苹果',
            hpbh:'KH20181112005',
            hpxh:'HX-12',
            hpdj:'12',
            hplb:'消耗品',
            zmkc:'112',
            sjkc:'100',
            hpdw:'个',
            bz:'没有备注',
            status:'使用中...',
            status1:'label bg-info',
            qy:false
        },
        {
            id:'1',
            createTime:'2018-11-12 10:30:39',
            hpmc:'苹果1',
            hpbh:'KH20181112005',
            hpxh:'HX-12',
            hpdj:'12',
            hplb:'消耗品',
            zmkc:'112',
            sjkc:'100',
            hpdw:'个',
            bz:'没有备注',
            status:'停用中...',
            status1:'label bg-danger',
            qy:false
        },
        {
            id:'1',
            createTime:'2018-11-12 10:30:39',
            hpmc:'苹果2',
            hpbh:'KH20181112005',
            hpxh:'HX-12',
            hpdj:'12',
            hplb:'消耗品',
            zmkc:'112',
            sjkc:'100',
            hpdw:'个',
            bz:'没有备注',
            status:'使用中...',
            status1:'label bg-info',
            qy:false
        },
        {
            id:'1',
            createTime:'2018-11-12 10:30:39',
            hpmc:'苹果3',
            hpbh:'KH20181112005',
            hpxh:'HX-12',
            hpdj:'12',
            hplb:'消耗品',
            zmkc:'112',
            sjkc:'100',
            hpdw:'个',
            bz:'没有备注',
            status:'使用中...',
            status1:'label bg-info',
            qy:true
        }
    ];
    $scope.showInfo = function(row){
        $scope.warehouseAdd = !($scope.warehouseAdd);
        $scope.warehouseList = !($scope.warehouseList);
        console.log(row);
        $scope.warhouseRow = row;
        $('#qy').prop("checked",$scope.warhouseRow.qy);
        $('#hplb').val($scope.warhouseRow.hplb);
    }
}])
;